package cn.iocoder.yudao.module.erp.controller.admin.business.money.moneypayunit;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.money.moneypayunit.vo.MoneyPayunitPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.money.moneypayunit.vo.MoneyPayunitRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.money.moneypayunit.vo.MoneyPayunitSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.money.moneypayunit.MoneyPayunitDO;
import cn.iocoder.yudao.module.erp.service.business.money.moneypayunit.MoneyPayunitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 付款单位")
@RestController
@RequestMapping("/erp/money-payunit")
@Validated
public class MoneyPayunitController {

    @Resource
    private MoneyPayunitService moneyPayunitService;

    @PostMapping("/create")
    @Operation(summary = "创建付款单位")
    @PreAuthorize("@ss.hasPermission('erp:money-payunit:create')")
    public CommonResult<Long> createMoneyPayunit(@Valid @RequestBody MoneyPayunitSaveReqVO createReqVO) {
        return success(moneyPayunitService.createMoneyPayunit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新付款单位")
    @PreAuthorize("@ss.hasPermission('erp:money-payunit:update')")
    public CommonResult<Boolean> updateMoneyPayunit(@Valid @RequestBody MoneyPayunitSaveReqVO updateReqVO) {
        moneyPayunitService.updateMoneyPayunit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除付款单位")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:money-payunit:delete')")
    public CommonResult<Boolean> deleteMoneyPayunit(@RequestParam("id") Long id) {
        moneyPayunitService.deleteMoneyPayunit(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得付款单位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:money-payunit:query')")
    public CommonResult<MoneyPayunitRespVO> getMoneyPayunit(@RequestParam("id") Long id) {
        MoneyPayunitDO moneyPayunit = moneyPayunitService.getMoneyPayunit(id);
        return success(BeanUtils.toBean(moneyPayunit, MoneyPayunitRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得付款单位分页")
    @PreAuthorize("@ss.hasPermission('erp:money-payunit:query')")
    public CommonResult<PageResult<MoneyPayunitRespVO>> getMoneyPayunitPage(@Valid MoneyPayunitPageReqVO pageReqVO) {
        PageResult<MoneyPayunitDO> pageResult = moneyPayunitService.getMoneyPayunitPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MoneyPayunitRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出付款单位 Excel")
    @PreAuthorize("@ss.hasPermission('erp:money-payunit:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMoneyPayunitExcel(@Valid MoneyPayunitPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MoneyPayunitDO> list = moneyPayunitService.getMoneyPayunitPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "付款单位.xls", "数据", MoneyPayunitRespVO.class,
                        BeanUtils.toBean(list, MoneyPayunitRespVO.class));
    }

}